package pl.un4giv3n.mightvsmagic.admin.action;

import java.util.Iterator;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import pl.un4giv3n.mightvsmagic.action.PersistenceAction;
import pl.un4giv3n.mightvsmagic.entity.ActivityType;
import pl.un4giv3n.mightvsmagic.entity.Item;
import pl.un4giv3n.mightvsmagic.entity.ItemTemplate;
import pl.un4giv3n.mightvsmagic.entity.ItemType;
import pl.un4giv3n.mightvsmagic.entity.User;
import pl.un4giv3n.mightvsmagic.entity.UserActivity;
import pl.un4giv3n.mightvsmagic.entity.UserType;
import pl.un4giv3n.mightvsmagic.utils.ActivityLogUtil;

/**
 * @author andrzejewskia
 * this action adds new items to the bazar
 * this action should be performed by admin ONLY
 */
public class EditItemAction extends PersistenceAction {

	private static final long serialVersionUID = 1L;
	
	private Long itemId;
	/**
	 * session user
	 */
	private User user;
	/**
	 * weapon name
	 */
	private String name;
	/**
	 * weapon type
	 */
	private ItemType type;
	/**
	 * url to the image
	 */
	private String imgUrl;
	/**
	 * item min def
	 */
	private double minDef;
	/**
	 * item max def
	 */
	private double maxDef;
	/**
	 * item min dmg
	 */
	private double minDmg;
	/**
	 * item max dmg
	 */
	private double maxDmg;
	/**
	 * item price
	 */
	private int price;
	private int premiumPrice;
	/**
	 * item strenght modifier
	 */
	private int strengthMod;
	/**
	 * item stamina modifier
	 */
	private int staminaMod;
	/**
	 * item mana modifier
	 */
	private int manaMod;
	/**
	 * item attack modifier
	 */
	private int attackMod;
	/**
	 * item parry modifier
	 */
	private int defenceMod;
	private int speedMod;
	/**
	 * item strenght Req
	 */
	private int strengthReq;
	/**
	 * item stamina Req
	 */
	private int staminaReq;
	/**
	 * item mana Req
	 */
	private int manaReq;
	/**
	 * item attack Req
	 */
	private int attackReq;
	/**
	 * item parry Req
	 */
	private int defenceReq;
	
	private long requiredFightExp;
	private long requiredMagicExp;
	private long requiredAnyExp;

	/* (non-Javadoc)
	 * @see pl.un4giv3n.mightvsmagic.action.PersistenceAction#execute()
	 */
	
	@Override
	public String execute() {
		EntityManager entityManager = getEntityManager();
		user = getSessionUser(entityManager);
		if (user == null || user.getUserType() != UserType.ADMIN) {
			addActionError("Zaloguj się");
			entityManager.close();
			return LOGIN;
		}
		entityManager.getTransaction().begin();
		ItemTemplate template;
		if(itemId != null){
			template = entityManager.find(ItemTemplate.class, itemId);
			updateTemplateAttrs(template);
			entityManager.merge(template);
			updateTemplateItems(entityManager, template);
			UserActivity activity = ActivityLogUtil.createActivity(ActivityType.ADMIN_EDITITEM, user.getId(), "Edycja przedmiotu: " + template.toString());
			entityManager.persist(activity);
		}else{
			template = new ItemTemplate(name, type, imgUrl, minDef,
					maxDef, minDmg, maxDmg, price, premiumPrice, requiredFightExp, requiredMagicExp,
					strengthMod, staminaMod, manaMod, attackMod, defenceMod, speedMod,
					strengthReq, staminaReq, manaReq, attackReq, defenceReq, requiredAnyExp);
			entityManager.persist(template);
		}
		entityManager.flush();
		entityManager.getTransaction().commit();
		entityManager.close();
		return SUCCESS;
	}

	private void updateTemplateItems(EntityManager entityManager,
			ItemTemplate template) {
		Query query = entityManager.createNamedQuery("find.template.items");
		query.setParameter("templateId", template.getId());
		@SuppressWarnings("unchecked")
		List<Item> items = query.getResultList();
		for (Iterator<Item> iterator = items.iterator(); iterator.hasNext();) {
			Item item = iterator.next();
			merge(item,template);
			entityManager.merge(item);
			
		}
	}

	private void updateTemplateAttrs(ItemTemplate template) {
		template.setName(name);
		template.setType(type);
		template.setImgUrl(imgUrl);
		template.setMinDef(minDef);
		template.setMaxDef(maxDef);
		template.setMinDmg(minDmg);
		template.setMaxDmg(maxDmg);
		template.setPrice(price);
		template.setPremiumPrice(premiumPrice);
		template.setRequiredFightExp(requiredFightExp);
		template.setRequiredMagicExp(requiredMagicExp);
		template.setStrenghtModifier(strengthMod);
		template.setStaminaModifier(staminaMod);
		template.setManaModifier(manaMod);
		template.setAttackModifier(attackMod);
		template.setParryModifier(defenceMod);
		template.setSpeedModifier(speedMod);
		template.setStrenghtReq(strengthReq);
		template.setStaminaReq(staminaReq);
		template.setManaReq(manaReq);
		template.setAttackReq(attackReq);
		template.setParryReq(defenceReq);
		template.setRequiredAnyExp(requiredAnyExp);
	}

	private void merge(Item item, ItemTemplate template) {
		item.setName(template.getName());
		item.setType(template.getType());
		item.setImgUrl(template.getImgUrl());
		item.setMinDef(template.getMinDef());
		item.setMaxDef(template.getMaxDef());
		item.setMinDmg(template.getMinDmg());
		item.setMaxDmg(template.getMaxDmg());
		item.setRequiredFightExp(template.getRequiredFightExp());
		item.setRequiredMagicExp(template.getRequiredMagicExp());
		item.setStrenghtModifier(template.getStrenghtModifier());
		item.setStaminaModifier(template.getStaminaModifier());
		item.setManaModifier(template.getManaModifier());
		item.setAttackModifier(template.getAttackModifier());
		item.setParryModifier(template.getParryModifier());
		item.setSpeedModifier(template.getSpeedModifier());
		item.setStrenghtReq(template.getStrenghtReq());
		item.setStaminaReq(template.getStaminaReq());
		item.setManaReq(template.getManaReq());
		item.setAttackReq(template.getAttackReq());
		item.setParryReq(template.getParryReq());
		item.setPrice(template.getPrice());
		item.setPremiumPrice(template.getPremiumPrice());
		item.setRequiredAnyExp(template.getRequiredAnyExp());
		
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public ItemType getType() {
		return type;
	}

	public void setType(ItemType type) {
		this.type = type;
	}

	public String getImgUrl() {
		return imgUrl;
	}

	public void setImgUrl(String imgUrl) {
		this.imgUrl = imgUrl;
	}

	public double getMinDef() {
		return minDef;
	}

	public void setMinDef(double minDef) {
		this.minDef = minDef;
	}

	public double getMaxDef() {
		return maxDef;
	}

	public void setMaxDef(double maxDef) {
		this.maxDef = maxDef;
	}

	public double getMinDmg() {
		return minDmg;
	}

	public void setMinDmg(double minDmg) {
		this.minDmg = minDmg;
	}

	public double getMaxDmg() {
		return maxDmg;
	}

	public void setMaxDmg(double maxDmg) {
		this.maxDmg = maxDmg;
	}

	public int getPrice() {
		return price;
	}

	public void setPrice(int price) {
		this.price = price;
	}

	public int getStrengthMod() {
		return strengthMod;
	}

	public void setStrengthMod(int strengthMod) {
		this.strengthMod = strengthMod;
	}

	public int getStaminaMod() {
		return staminaMod;
	}

	public void setStaminaMod(int staminaMod) {
		this.staminaMod = staminaMod;
	}

	public int getManaMod() {
		return manaMod;
	}

	public void setManaMod(int manaMod) {
		this.manaMod = manaMod;
	}

	public int getAttackMod() {
		return attackMod;
	}

	public void setAttackMod(int attackMod) {
		this.attackMod = attackMod;
	}

	public int getDefenceMod() {
		return defenceMod;
	}

	public void setDefenceMod(int defenceMod) {
		this.defenceMod = defenceMod;
	}

	public int getStrengthReq() {
		return strengthReq;
	}

	public void setStrengthReq(int strengthReq) {
		this.strengthReq = strengthReq;
	}

	public int getStaminaReq() {
		return staminaReq;
	}

	public void setStaminaReq(int staminaReq) {
		this.staminaReq = staminaReq;
	}

	public int getManaReq() {
		return manaReq;
	}

	public void setManaReq(int manaReq) {
		this.manaReq = manaReq;
	}

	public int getAttackReq() {
		return attackReq;
	}

	public void setAttackReq(int attackReq) {
		this.attackReq = attackReq;
	}

	public int getDefenceReq() {
		return defenceReq;
	}

	public void setDefenceReq(int defenceReq) {
		this.defenceReq = defenceReq;
	}

	public long getRequiredFightExp() {
		return requiredFightExp;
	}

	public void setRequiredFightExp(long requiredFightExp) {
		this.requiredFightExp = requiredFightExp;
	}

	public long getRequiredMagicExp() {
		return requiredMagicExp;
	}

	public void setRequiredMagicExp(long requiredMagicExp) {
		this.requiredMagicExp = requiredMagicExp;
	}

	public Long getItemId() {
		return itemId;
	}

	public void setItemId(Long itemId) {
		this.itemId = itemId;
	}

	public int getPremiumPrice() {
		return premiumPrice;
	}

	public void setPremiumPrice(int premiumPrice) {
		this.premiumPrice = premiumPrice;
	}

	public int getSpeedMod() {
		return speedMod;
	}

	public void setSpeedMod(int speedMod) {
		this.speedMod = speedMod;
	}

	public long getRequiredAnyExp() {
		return requiredAnyExp;
	}

	public void setRequiredAnyExp(long requiredAnyExp) {
		this.requiredAnyExp = requiredAnyExp;
	}

}
